import {
    shake
} from '../../../utils/util'
import {
    getPeopleListcopy,
    getPeopleList
} from '../../api/long-insure'
Page({
    data: {
        peoples: [],
        searchTypes: ['员工', '被派遣单位'],
        searchType: 0,
        type: '',
        back: '',
        selects: [],
        date: '', // 在保时间,
        companyPlanId: '',
        insurCompany: '',
        plant: '', // 是否换厂
        status: '',
        totalCount: 0,
        isba: false,
    },
    static: {
        pageNum: 1,
        hasNext: 1,
        search: '',
        searchType: 0
    },
    onLoad(options) {
        console.log(options, 'options')

        if (options.company) {
            this.setData({
                insurCompany: options.company
            })
        }
        if (options.status) {
            this.setData({
                status: options.status
            })
        }
        // plant=1&type=checkbox
        if (options.type && options.plant) {
            this.setData({
                type: options.type,
                plant: options.plant
            })
        }
        if (options.type && !options.plant) {
            this.setData({
                type: options.type,
                back: options.back,
                date: options.date
            })
        }
        if (options.id) {
            this.setData({
                companyPlanId: options.id
            })
        }
        if (options.isba) {
            this.setData({
                isba: true
            })
            this.getPeople()
        } else {
            this.setData({
                isba: false
            })
            this.getPeoplec()
        }
    },
    onReachBottom() {
        if (this.data.isba) {
            this.getPeople()
        } else {
            this.getPeoplec()
        }

    },
    onSearch(event) {
        if (this.data.isba) {
            shake(() => {
                this.static.search = event.detail.value
                this.getPeople(1)
            }, 800)
        } else {
            shake(() => {
                this.static.search = event.detail.value
                this.getPeoplec(1)
            }, 800)
        }
        // if(shake(event)){
        // }
    },
    // 报案人员列表
    async getPeople(clear) {
        if (clear) {
            this.static.pageNum = 1
            this.static.hasNext = true
            this.data.peoples = []
        }
        if (!this.static.hasNext) {
            return
        }
        const result = await getPeopleListcopy({
            companyPlanId: this.data.companyPlanId,
            pageNum: this.static.pageNum,
            pageSize: 20,
            searchUserInfo: this.data.searchType ? '' : this.static.search,
            searchServeCompany: this.data.searchType ? this.static.search : '',
            outDangerDate: this.data.date,
            insurCompany: this.data.insurCompany,
            // policyStatus:this.data.status || ''
            insurExistEndDateFlag: this.data.status ? true : false
        })
        console.log(result, 'resultresultresultresult')
        this.static.hasNext = result.hasNextPage
        this.static.pageNum += 1
        this.data.peoples.push(...result.list)
        this.setData({
            peoples: this.data.peoples,
            totalCount: result.totalCount
        })
    },
    // 所有
    async getPeoplec(clear) {
        if (clear) {
            this.static.pageNum = 1
            this.static.hasNext = true
            this.data.peoples = []
        }
        if (!this.static.hasNext) {
            return
        }
        const result = await getPeopleList({
            companyPlanId: this.data.companyPlanId,
            pageNum: this.static.pageNum,
            pageSize: 20,
            searchUserInfo: this.data.searchType ? '' : this.static.search,
            searchServeCompany: this.data.searchType ? this.static.search : '',
            outDangerDate: this.data.date,
            insurCompany: this.data.insurCompany,
            // policyStatus:this.data.status || ''
            insurExistEndDateFlag: this.data.status ? true : false
        })
        console.log(result, 'resultresultresultresult')
        this.static.hasNext = result.hasNextPage
        this.static.pageNum += 1
        this.data.peoples.push(...result.list)
        this.setData({
            peoples: this.data.peoples,
            totalCount: result.totalCount
        })
    },
    onTypeChange(event) {
        this.setData({
            searchType: event.detail.value
        })
    },
    onSelect(event) {
        const index = event.currentTarget.dataset.index
        const worker = this.data.peoples[index]
        if (this.data.type === 'radio') {
            this.data.selects = [worker]
        } else {
            const indexed = this.data.selects.indexOf(worker)
            console.log(indexed)
            if (indexed === -1) {
                this.data.selects.push(worker)
            } else {
                this.data.selects.splice(indexed, 1)
                console.log(this.data.selects)
            }
        }

        this.setData({
            selects: this.data.selects
        })
        console.log(this.data.selects)
    },
    onSubmit() {
        // 因逻辑需要而做出改变 在plant字段 ==1 时则使用换厂逻辑
        if (this.data.plant == 1) {
            console.log('plantplant')
        }
        if (this.data.isba) {
            if (this.data.selects.length===0) {
                wx.showToast({
                    title: '请选择人员',
                    icon: 'none'
                })
                return
            }
            var model = JSON.stringify(this.data.selects)
            wx.navigateTo({
                url: '/packageLongInsure/pages/report-add/report-add?model=' + model,
            })
        } else {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('complete', this.data.selects)
            if (!this.data.back) {
                wx.navigateBack(-1)
            }
        }
    }
})